package com.co.user.controller;

import com.co.common.entity.Result;
import com.co.user.pojo.User;
import com.co.user.service.UserService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author ldl.plus
 * @date 2020年06月22日  0:41
 */

@RestController
@RequestMapping("/user")

public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 登录
     *
     * @param user
     * @return
     */
    @RequestMapping("/login")
    public Result login(@RequestBody User user) {
        boolean login = userService.login(user);
        if (login) {
            return Result.ok("登录成功");
        } else {
            return Result.failed("登录失败");
        }
    }

    @RequestMapping("/signup")
    public Result signup(@RequestBody User user) {
        try {
            userService.add(user);
            return Result.ok("注册成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed("注册失败");
        }
    }


    /**
     * UsUser分页条件搜索实现
     *
     * @param user
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/search/{page}/{size}")
    public Result<PageInfo<User>> findPage(@RequestBody(required = false) User user, @PathVariable int page, @PathVariable int size) {
        //调用UsUserService实现分页条件查询UsUser
        PageInfo<User> pageInfo = userService.findPage(user, page, size);
        return Result.ok(pageInfo);
    }

    /**
     * UsUser分页搜索实现
     *
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}")
    public Result<PageInfo<User>> findPage(@PathVariable int page, @PathVariable int size) {
        //调用UsUserService实现分页查询UsUser
        PageInfo<User> pageInfo = userService.findPage(page, size);
        return Result.ok(pageInfo);
    }

    /**
     * 多条件搜索品牌数据
     *
     * @param user
     * @return
     */
    @PostMapping(value = "/search")
    public Result<List<User>> findList(@RequestBody(required = false) User user) {
        //调用UsUserService实现条件查询UsUser
        List<User> list = userService.findList(user);
        return Result.ok(list);
    }

    /**
     * 根据ID删除品牌数据
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public Result<?> delete(@PathVariable String id) {
        //调用UsUserService实现根据主键删除
        userService.delete(id);
        return Result.ok();
    }

    @PutMapping(value = "/detail/{username}")
    public Result<Object> updateDetail(@RequestBody User user, @PathVariable String username) {

        User user1 = userService.findById(username);

        userService.update(user);
        return Result.ok();
    }

    /**
     * 修改UsUser数据
     *
     * @param map
     * @param username
     * @return
     */
    @PutMapping(value = "/update/{username}")
    public Result<Object> update(@RequestBody Map<String, String> map, @PathVariable String username) {
        String salt = BCrypt.gensalt();
        //设置主键值
        User user = userService.findById(username);
        //获取对象值（oldPassword\newPassword\confirmPassword）
        String oldPassword = map.get("oldPassword");
        String newPassword = map.get("newPassword");

        if (StringUtils.isEmpty(oldPassword)) {
            return Result.failed("请输入旧密码");
        }
        if (StringUtils.isEmpty(newPassword)) {
            return Result.failed("请输入新密码");
        }

        //数据库数据
        String password = user.getPassword();

        //判断用户输入数据与数据库数据
        boolean flag = BCrypt.checkpw(oldPassword, password);
        if (!flag) {
            return Result.failed("旧密码输入错误");
        } else {
            //生成盐
            //对用户密码进行加密
            String hashpw = BCrypt.hashpw(newPassword, salt);
            user.setPassword(hashpw);
            user.setUpdateTime(new Date());
        }
        //调用UsUserService实现修改UsUser
        userService.updateUserPassword(user);
        return Result.ok();
    }

    /**
     * 新增UsUser数据
     *
     * @param user
     * @return
     */
    @PostMapping
    public Result<Object> add(@RequestBody User user) {
        //调用UsUserService实现添加UsUser
        userService.add(user);
        return Result.ok();
    }

    /**
     * 根据ID查询UsUser数据
     *
     * @param username
     * @return
     */
    @GetMapping("/{username}")
    public Result<User> findById(@PathVariable String username) {
        //调用UsUserService实现根据主键查询UsUser
        User user = userService.findById(username);
        return Result.ok(user);
    }

    /**
     * 查询UsUser全部数据
     *
     * @return
     */
    @GetMapping
    public Result<List<User>> findAll() {
        //调用UsUserService实现查询所有UsUser
        List<User> list = userService.findAll();
        return Result.ok(list);
    }

    /**
     * 根据用户名查询用户信息
     *
     * @param username
     * @return
     */
    @GetMapping("/load/{username}")
    public User findUserInfo(@PathVariable("username") String username) {
        User user = userService.findById(username);
        return user;
    }

    @GetMapping("/sub/{username}")
    public Result findSubsByUsername(@PathVariable("username") String username) {
        List<String> subsByUsername = userService.findSubsByUsername(username);
        return Result.ok(subsByUsername);
    }
}
